package com.hs.biye.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hs.biye.constant.MessageConstant;
import com.hs.biye.excepction.BaseException;
import com.hs.biye.mapper.*;
import com.hs.biye.model.DTO.BedRequest;
import com.hs.biye.model.DTO.BedRequest;
import com.hs.biye.model.VO.BedVo;
import com.hs.biye.model.VO.BedVo;
import com.hs.biye.model.entity.*;
import com.hs.biye.model.entity.Bed;
import com.hs.biye.model.reslut.ResultPage;
import com.hs.biye.service.BedService;
import com.hs.biye.mapper.BedMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author haoshuai
 * @description 针对表【bed】的数据库操作Service实现
 * @createDate 2024-02-18 12:36:37
 */
@Service
public class BedServiceImpl extends ServiceImpl<BedMapper, Bed>
        implements BedService {

    @Resource
    private BedMapper bedMapper;

    @Resource
    private PatientMapper patientMapper;

    @Resource
    private EmployeeMapper employeeMapper;

    @Resource
    private RoomMapper roomMapper;


    @Override
    public ResultPage<BedVo> getPageBed(BedRequest bedRequest) {
        if (bedRequest == null) {
            throw new BaseException(MessageConstant.PAR_NOT_RIGHT);
        }
        Integer page = bedRequest.getPages();
        Integer size = bedRequest.getSize();
        IPage<Bed> pages = new Page<>(page, size);
        LambdaQueryWrapper<Bed> queryWrapper = getQueryWrapper(bedRequest);
        bedMapper.selectPage(pages, queryWrapper);

        List<Bed> records = pages.getRecords();
        long total = pages.getTotal();
        long current = pages.getCurrent();
        long page2 = pages.getPages();

        List<BedVo> reslutList = new ArrayList<>();
        records.forEach(item -> {
            String employeeName = "空";
            String patientName = "空";
            if (item.getStatue() == 1) {
                //查询病人信息
                Patient patient = patientMapper.selectById(item.getPatientId());
                //查询员工信息
                Employee employee = employeeMapper.selectById(item.getDoctorId());
                employeeName = employee.getEmployeeName();
                patientName = patient.getPatientName();

            }
            //查询房间号
            Room room = roomMapper.selectById(item.getRoomId());
            String  roomNumber = room.getRoomNumber();
            //封装vo
            BedVo bedVo = new BedVo();
            BeanUtils.copyProperties(item, bedVo);
            bedVo.setDoctorName(employeeName);
            bedVo.setPatientName(patientName);
            bedVo.setRoomNumber(roomNumber);
            reslutList.add(bedVo);
        });
        return ResultPage.success(reslutList, total, current, page2);
    }


    //删除
    @Override
    public boolean removeBed(Integer id) {
        if (id == null) {
            throw new BaseException(MessageConstant.PAR_NOT_RIGHT);
        }
        return removeById(id);
    }


    //添加
    @Override
    public boolean addBed(BedRequest bedRequest) {
        if (bedRequest == null) {
            throw new BaseException(MessageConstant.PAR_NOT_RIGHT);
        }

        Integer roomId = bedRequest.getRoomId();
        Integer bedNum = bedRequest.getBedNum();

        Bed bed = new Bed();
        bed.setRoomId(roomId);
        bed.setBedNum(bedNum);
        return save(bed);
    }

    @Override
    public boolean updateBed(BedRequest bedRequest) {
        if (bedRequest == null) {
            throw new BaseException(MessageConstant.PAR_NOT_RIGHT);
        }
        Integer bedId = bedRequest.getBedId();
        Integer bedNum = bedRequest.getBedNum();
        Integer roomId = bedRequest.getRoomId();

        Bed bed = new Bed();
        bed.setBedId(bedId);
        bed.setBedNum(bedNum);
        bed.setRoomId(roomId);
        return updateById(bed);
    }


    //根据roomId查询病床号
    public  List<Map<String, Integer>> getBedNum(Integer roomId){
        List<Integer> Nums = Arrays.asList(1, 2, 3, 4, 5, 6);
        List<Integer> list1 = new ArrayList<>(Nums);
        List<Integer> list2 = new ArrayList<>();
        LambdaQueryWrapper<Bed> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(Bed::getBedNum).eq(Bed::getRoomId,roomId);
        List<Bed> bedList = bedMapper.selectList(queryWrapper);
        bedList.forEach(item->{
            list2.add(item.getBedNum());
        });

        list1.removeAll(list2);
        List<Map<String, Integer>> res = new ArrayList<>();
        list1.forEach(item->{
            Map<String, Integer> map = new HashMap<>();
            map.put("bedNum",item);
            res.add(map);
        });
        return res;
    }


    //获取id和未使用的床位号
    @Override
    public List<BedVo> getBedNumAdnBedId(Integer roomId) {
        LambdaQueryWrapper<Bed> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(Bed::getBedId,Bed::getBedNum)
                        .eq(Bed::getStatue,0).eq(Bed::getRoomId,roomId);
        List<Bed> bedList = bedMapper.selectList(queryWrapper);
        List<BedVo> res = new ArrayList<>();
        bedList.forEach(it->{
            BedVo bedVo = new BedVo();
            bedVo.setBedNum(it.getBedNum());
            bedVo.setBedId(it.getBedId());
            res.add(bedVo);
        });
        return res;
    }

    @Override
    public BedVo getOneById(Integer id) {
        if (id == null) {
            throw new BaseException(MessageConstant.PAR_NOT_RIGHT);
        }
        Bed bed = getById(id);
        BedVo bedVo = new BedVo();
        BeanUtils.copyProperties(bed,bedVo);
        return bedVo;
    }


    private LambdaQueryWrapper<Bed> getQueryWrapper(BedRequest bedRequest) {
        LambdaQueryWrapper<Bed> medicineLambdaQueryWrapper = new LambdaQueryWrapper<>();
        Integer bedld = bedRequest.getBedId();
        Integer patientId = bedRequest.getPatientId();
        Integer roomId = bedRequest.getRoomId();
        Integer bedNum = bedRequest.getBedNum();
        Integer doctorId = bedRequest.getDoctorId();
        LocalDateTime createTime = bedRequest.getCreateTime();
        Integer statue = bedRequest.getStatue();
        LocalDateTime updateTime = bedRequest.getUpdateTime();
        Integer pages = bedRequest.getPages();
        Integer size = bedRequest.getSize();
        medicineLambdaQueryWrapper.eq(ObjectUtil.isNotEmpty(statue), Bed::getStatue, statue);
        medicineLambdaQueryWrapper.eq(ObjectUtil.isNotEmpty(roomId), Bed::getRoomId, roomId);
        //排序
        // employeeQueryWrapper.orderBy(true,true,Employee::getCreateTime);

        return medicineLambdaQueryWrapper;
    }
}




